# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=soupault
pkgver=4.11.0
pkgrel=0
pkgdesc="Static website generator based on HTML element tree rewriting"
url="https://soupault.app/"
arch="all"
license="MIT"
makedepends="
	dune
	ocaml5-compiler-libs
	opam-monorepo
	"
subpackages="$pkgname-doc"
source="https://github.com/PataphysicalSociety/soupault/archive/refs/tags/$pkgver/soupault-$pkgver.tar.gz
	soupault.opam.locked
	"

export OPAMROOT="${OPAMROOT:-"$srcdir/opam-root"}"
export OPAMLOGS="${OPAMLOGS:-"$srcdir/opam-logs"}"

_opam_opts="-v -y --cli=2.2 --no-self-upgrade"
_lockfile_name="$pkgname.opam.locked"
_monorepo_opts="-v --lockfile=$_lockfile_name"

_minimal_update=true
recreate() {
	_minimal_update=false
}

opam_init() {
	opam init $_opam_opts \
		--compiler=ocaml-system \
		--disable-sandboxing \
		--no-setup
}

opam_lock() {
	[ -f "$OPAMROOT"/config ] || {
		opam_init

		opam repository $_opam_opts \
			add dune-universe \
			git+https://github.com/dune-universe/opam-overlays.git
	}

	cd "$startdir"
	[ -d "$builddir" ] || abuild unpack
	msg "Freezing $pkgname dependencies"

	# Resolve deps and update $pkgname.opam.locked, unless
	# recreate() is called, in which case create it afresh.
	(
		cd "$builddir" || {
			error 'Is $builddir set correctly?'
			return 1
		}
		opam update $_opam_opts

		if ! want_check; then
			_monorepo_opts="$_monorepo_opts --build-only"
		fi
		if $_minimal_update; then
			cp -v "$srcdir"/$_lockfile_name .

			_monorepo_opts="$_monorepo_opts --minimal-update"
		fi
		opam-monorepo lock $_monorepo_opts

		mv -v $_lockfile_name "$startdir"/
	)

	if ! abuild checksum; then
		die "Failed to update checksum, run 'abuild checksum' manually"
	fi
}

prepare() {
	default_prepare

	[ -f "$OPAMROOT"/config ] || opam_init
	ln -svf "$srcdir"/$_lockfile_name "$builddir"/
	opam-monorepo pull $_monorepo_opts --yes
}

build() {
	dune build --release --verbose
}

check() {
	dune runtest --build-dir=.testenv
}

package() {
	dune install --destdir="$pkgdir" --sections=bin,doc

	mkdir -vp "$pkgdir"/usr/share/doc/$pkgname
	cp -vr examples sample-site "$pkgdir"/usr/share/doc/$pkgname/
}

sha512sums="
0404890e42626986914e22376ee5c2998290e03164ce1a6ff4d5510633a2f6783c9b6a8f545a96222d93ab24fc057af5d8aa494d3d5320ec3bcd23fc3eedec7b  soupault-4.11.0.tar.gz
8d0212e056caa979ac4e3b848b727beb39b4fee69c1f9fdffa437f4412b4fa1a4524536e043a74ecfacd2e5c8ffded2941c82d4c8bca992daf31c5e29765d0e6  soupault.opam.locked
"
